package com.jiangjun.fetch.servlet;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.Timer;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.DOMReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.jiangjun.fetch.FetchWeibo;
import com.jiangjun.fetch.common.ProloadConfigure;
import com.jiangjun.fetch.quartz.FetchTask;
import com.jiangjun.fetch.quartz.FetchWeiboTask;

@WebListener
public class Start implements ServletContextListener {

	private final Logger logger = LoggerFactory.getLogger(Start.class);

	private static final String[] locations = { "application.xml" };

	public static ClassPathXmlApplicationContext factory;

	public static String path = "/usr/surf";
	
	public static String imgServer;

	public static ArrayList<String> screen;

	public static String SURF_CONFIG_PATH = "SurfConfig.xml";

	public static ArrayList<String> removeAttributes;

	public static ArrayList<String> removeElementTags;

	public Timer timer = new Timer();
	
	public Timer timer4weibo = new Timer();

	@Override
	public void contextInitialized(ServletContextEvent arg0) {

		removeAttributes = new ArrayList<String>();
		removeElementTags = new ArrayList<String>();
		if (SURF_CONFIG_PATH != null) {
			DocumentBuilderFactory domfac = null;
			DocumentBuilder dombuilder = null;
			InputStream inputStream = null;
			DOMReader dr = new DOMReader();
			try {
				domfac = DocumentBuilderFactory.newInstance();
				dombuilder = domfac.newDocumentBuilder();
				inputStream = ProloadConfigure.class.getClassLoader()
						.getResourceAsStream(SURF_CONFIG_PATH);
				org.w3c.dom.Document doc = dombuilder.parse(inputStream);
				Document document = dr.read(doc);
				Element root = document.getRootElement();
				Element remove_ele = root.element("removeElementTags");
				Element remove_att = root.element("removeAttributes");
				List<Element> remove_eles = remove_ele
						.selectNodes("removeElementTag");
				List<Element> remove_atts = remove_att
						.selectNodes("removeAttribute");
				for (Element ele : remove_eles) {
					removeElementTags.add(ele.getText().trim());
				}
				for (Element ele : remove_atts) {
					removeAttributes.add(ele.attributeValue("name"));
				}
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}

		}

		screen = new ArrayList<String>();
		InputStream is = null;
		try {
			is = ProloadConfigure.class.getClassLoader().getResourceAsStream(
					"config.properties");
			Properties p = new Properties();
			p.load(is);
			path = p.getProperty("path");
			imgServer = p.getProperty("imgserver");
			String[] screen_array = p.getProperty("screen").split(",");
			for (String s : screen_array) {
				screen.add(s);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (is != null) {
				try {
					is.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		factory = new ClassPathXmlApplicationContext(locations);
		timer.schedule(new FetchTask(), 0, 30 * 60 * 1000);
		timer.schedule(new FetchWeiboTask(), 0, 10 * 60 * 1000);
	}

	@Override
	public void contextDestroyed(ServletContextEvent arg0) {
		// TODO Auto-generated method stub

	}
}
